/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.fatecmm.cadastrofilmes.servlets;

import br.edu.fatecmm.cadastrofilmes.banco.FilmesDAO;
import br.edu.fatecmm.cadastrofilmes.banco.GeneroDAO;
import br.edu.fatecmm.cadastrofilmes.beans.BnFilmes;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author raphael
 */
public class SltFilmes extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
      operacao = request.getParameter("operacao");
        chave = request.getParameter("chave");

        RequestDispatcher rd = null;
        
        if("incluir".equals(operacao))
        {
            GeneroDAO generoDAO;
            try
            {
                generoDAO = new GeneroDAO();
                List itens = generoDAO.todos();
                request.setAttribute("itens", itens);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            rd = request.getRequestDispatcher("/incluirFilmes.jsp");
        }
        else if("gravarinclusao".equals(operacao))
        {
            bnFilmes = new BnFilmes();
            bnFilmes.setNome(request.getParameter("nome"));
            //conversão de STRING  para INTEGER
            bnFilmes.setGenero(Integer.parseInt(request.getParameter("genero")));
            bnFilmes.setDuracao(request.getParameter("duracao"));
            bnFilmes.setAno(request.getParameter("ano"));

            try
            {
                filmesDAO = new FilmesDAO();
                filmesDAO.salvar(bnFilmes);

                rd = request.getRequestDispatcher("SltGenero?operacao=listar");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("editar".equals(operacao))
        {
            try
            {
                bnFilmes = new BnFilmes();
                filmesDAO = new FilmesDAO();
                bnFilmes = filmesDAO.procurar(Integer.parseInt(chave));
                HttpSession session = request.getSession(true);
                session.setAttribute("bnFilmes", bnFilmes);
                rd = request.getRequestDispatcher("/alterarFilmes.jsp");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("gravaralteracao".equals(operacao))
        {
            bnFilmes = new BnFilmes();
            bnFilmes.setId(Integer.parseInt(request.getParameter("id")));
            bnFilmes.setGenero(Integer.parseInt(request.getParameter("genero")));
            bnFilmes.setDuracao(request.getParameter("duracao"));
            bnFilmes.setAno(request.getParameter("ano"));
            
            try
            {
                filmesDAO = new FilmesDAO();
                filmesDAO.atualizar(bnFilmes);

                rd = request.getRequestDispatcher("SltFilmes?operacao=listar");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("listar".equals(operacao))
        {
            try
            {
                filmesDAO = new FilmesDAO();
                List itens = filmesDAO.todos();
                request.setAttribute("itens", itens);
                rd = request.getRequestDispatcher("/filmesListar.jsp");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("excluir".equals(operacao))
        {
            try
            {
                bnFilmes = new BnFilmes();
                filmesDAO = new FilmesDAO();
                bnFilmes.setId(Integer.parseInt(chave));
                filmesDAO.excluir(bnFilmes);
                rd = request.getRequestDispatcher("StlFilmes?operacao=listar");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        

        if(rd != null)
            rd.forward(request, response);
    
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    
    private String operacao;
    private String chave;
    private BnFilmes bnFilmes;
    private FilmesDAO filmesDAO;
}
